package JiHe;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Test7 {
    public static void main(String[] args) {
        List<String> list = new ArrayList<String>();
        list.add("b");
        list.add("c");
        list.add("d");
        list.add("1");
        list.add("10");
        System.out.println(list);
        //反转list集合
        Collections.reverse(list);
        System.out.println(list);
        //对list集合随机排序
        Collections.shuffle(list);
        System.out.println(list);
        //对list集合升序排列
        Collections.sort(list);
        System.out.println(list);
        //替换元素的位置
        Collections.swap(list,0,4);
        System.out.println(list);
        //返回集合中最大的元素
         System.out.println(Collections.max(list));
        //返回集合中最小的元素
        System.out.println(Collections.min(list));
        //指定元素出现在集合中的次数
        System.out.println(Collections.frequency(list,"a"));
        //替换集合中指定的值
        Collections.replaceAll(list,"a","aa");
        Student s1 = new Student(12,"张三");
        Student s2 = new Student(13,"李四");
        Student s3 = new Student(2,"王五");
        Student s4 = new Student(62,"小六");
        List<Student> stu = new ArrayList<Student>();
        stu.add(s1);
        stu.add(s2);
        stu.add(s3);
        stu.add(s4);
        for(Student i:stu){
            System.out.println(i.name + i.age);
        }
        Collections.sort(stu,new Student());
        for(Student i:stu){
            System.out.println(i.name + i.age);
        }

    }
}
class Student implements Comparator<Student>{
    int age;
    String name;

    public Student(int age, String name) {
        this.age = age;
        this.name = name;
    }

    public Student() {
    }

    @Override
    public int compare(Student o1, Student o2) {
        if(o1.age>o2.age){
            return 1;
        }else if(o1.age<o2.age){
            return -1;
        }else{
            return 0;
        }
    }
}